package com.nageoffer.shortlink.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.nageoffer.shortlink.admin.common.convention.exception.ClientException;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.common.enums.UserErrorCodeEnum;
import com.nageoffer.shortlink.admin.dto.req.UserLoginReqDTO;
import com.nageoffer.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.nageoffer.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.nageoffer.shortlink.admin.dto.resp.UserActualRespDTO;
import com.nageoffer.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.nageoffer.shortlink.admin.dto.resp.UserRespDTO;
import com.nageoffer.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user")
    public Result<UserRespDTO> getUserByUsername(@RequestParam("username") String username) {
        log.info("getUserByUsername: {}", username);
        UserRespDTO result = userService.getUserByUsername(username);
        if (result == null) {
            throw new ClientException(UserErrorCodeEnum.USER_IS_NOT_EXSIT);
        } else
            return Results.success(result);
    }

    @GetMapping("/api/short-link/admin/v1/actual/user")
    public Result<UserActualRespDTO> ActualgetUserByUsername(@RequestParam("username") String username) {
        log.info("getUserByUsername: {}", username);
        UserRespDTO result = userService.getUserByUsername(username);
        if (result == null) {
            throw new ClientException(UserErrorCodeEnum.USER_IS_NOT_EXSIT);
        } else
            return Results.success(BeanUtil.toBean(result, UserActualRespDTO.class));
    }

    @GetMapping("/api/short-link/admin/v1/user/hasUsername")
    public Result<Boolean> availableUsername(@RequestParam("username") String username) {
        log.info("hasUsername: {}", username);
        return Results.success(userService.availableUsernmae(username));
    }

    /**
     * 用户注册
     * @param reqParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO reqParam) {
        userService.register(reqParam);
        return Results.success();
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> updateUserInfo(@RequestBody UserUpdateReqDTO reqParam) {
        //TODO: 2025/4/26 验证用户名是否重复
        userService.updateUserInfo(reqParam);
        return Results.success();
    }

    /**
     * 用户登录功能
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO reqParam) {
        return Results.success(userService.login(reqParam));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    @DeleteMapping("/api/short-link/admin/v1/user")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username, token);
        return Results.success();
    }

}

